﻿using AutoMapper;
using MediatR;
using S.Organization.API.Application.Command.CottageCommand;
using S.Organization.Domian.Model;
using S.Organization.Infrastructure;

namespace S.Organization.API.Application.CommandHandler.CottageCommandHandler
{
    public class DelCottageCommandHandler : IRequestHandler<DelCottageCommand, int>
    {
        private readonly IBaseRepository<CottageSetting> _repository;
        private readonly IBaseRepository<FieldSetting> r;
        private readonly IMapper mapper;

        public DelCottageCommandHandler(IBaseRepository<CottageSetting> repository, IBaseRepository<FieldSetting> r,IMapper mapper)
        {
            _repository = repository;
            this.mapper = mapper;
            this.r = r;
        }

        public async Task<int> Handle(DelCottageCommand request, CancellationToken cancellationToken)
        {
            var list = await r.GetAllAsync();
            
            foreach (var item in list)
            {
                if(request.CottageId==item.CottageId)
                {
                    await r.DeleteAsync(item.FieldId);
                } 
            }

            return await _repository.DeleteAsync(request.CottageId);
        }
    }
}
